#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

namespace FlashBack.Web
{
	using System;
	using System.Web;
	using System.Web.UI;
	using System.ComponentModel;
	using System.Data;
	using System.Security;
	using AirWeb.Security;
	using AirWeb.Support.Diagnostics;
	using AirWeb.ApplicationTools;

	/// <summary>
	///     The code-behind base class for all pages.
	///     <remarks>
	///         This class derives off of System.Web.UI.Page.
	///     </remarks>
	/// </summary>
	public class PageBase : System.Web.UI.Page
	{
		//
		// Exception Logging constant
		//
		private const String UNHANDLED_EXCEPTION = "Unhandled Exception:";

		//Progress bar class to indicate a long operation
		//subclasses my use it to indicate a long operation
		protected ServerProgressBar m_progBar;

		/// <summary>
		///     Constructor for PageBase. 
		///     <remarks>Initialize private members.</remarks>
		/// </summary>
		public PageBase ()
		{
		}

		/// <summary>
		/// Validates that the curren user is a valid user of the system
		/// </summary>
		/// <exception cref="SecurityException">If the the current user is invalid</exception>
		protected void ValidateCurrentUser()
		{
			if(CurrentUser == null)
			{
				throw new SecurityException("Attempt to access a protected page without a valid user");
			}
		}

		/// <summary>
		/// Validates that the curren user is a valid user of the system and is a memeber of the administrators group
		/// </summary>
		/// <exception cref="SecurityException">If the the current user is invalid</exception>
		protected void ValidateCurrentUserIsAdministrator()
		{
			ValidateCurrentUser();
			if(!CurrentUser.IsAdministrator())
			{
				throw new SecurityException("The current user is not an Administrator");
			}
		}

		/// <summary>
		/// This is the user making the request for this page
		/// </summary>
		protected User CurrentUser
		{
			get
			{
				User u = (User)Session["KEY_CACHE_CURRENT_USER"];
				return u;
			}
		}
       
		/// <summary>
		///     Handles errors that may be encountered when displaying this page.
		///     <param name="e">An EventArgs that contains the event data.</param>
		/// </summary>
		protected override void OnError(EventArgs e) 
		{
			ApplicationLog.WriteError(ApplicationLog.FormatException(Server.GetLastError(), UNHANDLED_EXCEPTION));
			base.OnError(e);
		}

		/// <summary>
		/// This method hadles events sent by the Directory Helper
		/// while generating new pictures. It displays a progress bar
		/// showing the progress of the generation
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void DirHelperNotifier(object sender, DirHelperStatusEvent e)
		{
			switch(e.Type)
			{
				case DirHelperStatusEvent.EVT_TYPES.READY_TO_GENERATE:
					if(m_progBar == null && e.Number > 5) // We have at least 5 files to generate
					{
						m_progBar = new ServerProgressBar(0,e.Number,5,30,
							"green","file:ProgressHolder.htm","ProgressHolder");

						m_progBar.ScriptEnginePath = @"cjs/ProgressBar.js";
						m_progBar.RenderClientScript();
					}
					break; 

				case DirHelperStatusEvent.EVT_TYPES.GENERATING:
					if(m_progBar != null && e.Number % 5 == 0)
							m_progBar.Step();
					break;

				case DirHelperStatusEvent.EVT_TYPES.DONE_GENERATING:
					if(m_progBar != null)
						m_progBar.CleanProgressBar();
					break;

				default:
					break;
			}
		}
	} 
}